package com.bilin.coin.list;

import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.bilin.coin.ApiConstant;
import com.bilin.coin.R;
import com.bilin.coin.bean.CoinBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.utils.DensityUtils;
import com.bilin.coin.utils.GlideUtils;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.ListUtils;
import com.bilin.coin.utils.SpacesItemDecoration;
import com.bilin.coin.widget.AbsPullToRefreshRecycleView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CoinList extends AbsPullToRefreshRecycleView<CoinBean> {

    int type;

    public CoinList(BaseAppCompatActivity activity, int type) {
        super(activity, true, false, false);
        this.type = type;
    }

    @Override
    public int fillPageSize() {
        return 15;
    }

    @Override
    public boolean isPost() {
        return false;
    }

    @Override
    public List parseListDataAndFillTotal(String dataJson) {
        List<CoinBean> productHomeBean = GsonUtil.jsonToList(dataJson, CoinBean.class);
        productHomeBean = ListUtils.filter(productHomeBean, new ListUtils.ListUtilsHook<CoinBean>() {
            @Override
            public boolean test(CoinBean coinBean) {
                if (type == 0) {
                    return coinBean.isCharge();
                }else if (type==1) {
                    return coinBean.isWithdraw();
                } else {
                    return true;
                }
            }
        });
        totalCount = productHomeBean.size();
        return productHomeBean;
    }

    @Override
    public Map<String, String> fillParams() {
        HashMap params = new HashMap();
        params.put("currentPage", page + "");
        params.put("pageSize", pageSize + "");
        return params;
    }

    @Override
    protected RecyclerView.LayoutManager fillLayoutManger() {
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.addItemDecoration(new SpacesItemDecoration(1, DensityUtils.dp2px(activity, 10), 0x1888));
        return manager;
    }

    @Override
    public String fillUrl() {
        return type==2 ? "/api/coin/v2/exchangeList":"/api/coin/list";
    }

    @Override
    protected void convertItem(BaseViewHolder helper, CoinBean item) {
        if (type == 2) {
            helper.setText(R.id.tv_name, item.getTitle());
        } else {
            helper.setText(R.id.tv_name, item.getName());
        }
        ImageView iv_coin = helper.getView(R.id.iv_coin);
        GlideUtils.getInstance().displayCircleImage(activity, ApiConstant.OSSURL+item.getUrl(), iv_coin);
    }

    @Override
    protected int fillAnimationType() {
        return BaseQuickAdapter.ALPHAIN;
    }

    @Override
    protected int fillItemLayout() {
        return R.layout.item_coin;
    }
}
